package com.hcbd.tdwt.ui.fuda.common.adapter;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.DisasterEarlyWarning;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.DisasterLevelType;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.DisasterWarnType;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.RealTimeWeather;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.UltravioletLevelType;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WeatherCodeType;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WeatherForecast;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WeatherInfo;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WeatherModeType;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WindDirType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.hcbd.tdwt.util.AppUtil;
import com.hcbd.tdwt.util.DateUtils;

import java.util.List;


/**
 * - @Description:  天气列表适配器
 * - @Author:  LXJ
 * - @Time:  2019/7/30 9:54
 */
public class WeatherAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<WeatherInfo> mList;
    private LayoutInflater mLayoutInflater;
    private OnItemClickListener onItemClickListener;
    //收到消息
    private static final int RECEIVE_MESSAGE = 0;
    //发出消息
    private static final int SEND_MESSAGE = 1;

    public WeatherAdapter(Context context, List<WeatherInfo> list) {
        this.mContext = context;
        this.mList = list;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == RECEIVE_MESSAGE) {
            return new ReceiveItemViewHolder(mLayoutInflater.inflate(R.layout.weather_receive_item, parent, false));
        } else {
            return new SendItemViewHolder(mLayoutInflater.inflate(R.layout.weather_send_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        WeatherInfo weatherInfo = mList.get(position);
        if (holder instanceof ReceiveItemViewHolder) {
            if (weatherInfo.getWeatherMode() == WeatherModeType.RealTime.getValue()) {
                ((ReceiveItemViewHolder) holder).contentLayout.removeAllViews();
                ((ReceiveItemViewHolder) holder).contentLayout.setVisibility(View.VISIBLE);
                //实时天气
                ((ReceiveItemViewHolder) holder).titleLayout.setVisibility(View.VISIBLE);
                ((ReceiveItemViewHolder) holder).tipsTv.setVisibility(View.GONE);
                ((ReceiveItemViewHolder) holder).titleImg.setImageResource(R.mipmap.dot_blue_icon);
                ((ReceiveItemViewHolder) holder).titleTv.setText(WeatherModeType.RealTime.getName());
                ((ReceiveItemViewHolder) holder).titleTv.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
                ((ReceiveItemViewHolder) holder).timeTv.setVisibility(View.VISIBLE);
                ((ReceiveItemViewHolder) holder).timeTv.setText(weatherInfo.getCreateTime());
                //将json数据转换成实体类
                Gson gson = new Gson();
                RealTimeWeather realTimeWeather = gson.fromJson(weatherInfo.getJsonInfo(), RealTimeWeather.class);
                addTextToView(mContext.getString(R.string.temperature), realTimeWeather.getTmp() + "°C", ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.live_weather), WeatherCodeType.get(realTimeWeather.getCondCode()).getName(), ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.wind_direction), WindDirType.get(realTimeWeather.getWindDir()).getName(), ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.wind), realTimeWeather.getWindSc() + "m/s", ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.wind_speed), realTimeWeather.getWindSpd() + "km/h", ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.relative_humidity), String.valueOf(realTimeWeather.getHum()), ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.rainfall), realTimeWeather.getPcpn() + "mm", ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.atmospheric_pressure), realTimeWeather.getPres() + "hPa", ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.visibility), realTimeWeather.getVisable() + "km", ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.cloud_cover), String.valueOf(realTimeWeather.getCloud()), ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.adders), realTimeWeather.getAdress(), ((ReceiveItemViewHolder) holder).contentLayout);
            } else if (weatherInfo.getWeatherMode() == WeatherModeType.Forecast.getValue()) {
                ((ReceiveItemViewHolder) holder).contentLayout.removeAllViews();
                ((ReceiveItemViewHolder) holder).contentLayout.setVisibility(View.VISIBLE);
                //天气预报
                ((ReceiveItemViewHolder) holder).titleLayout.setVisibility(View.VISIBLE);
                ((ReceiveItemViewHolder) holder).tipsTv.setVisibility(View.GONE);
                ((ReceiveItemViewHolder) holder).titleImg.setImageResource(R.mipmap.dot_green_icon);
                ((ReceiveItemViewHolder) holder).titleTv.setText(WeatherModeType.Forecast.getName());
                //((ReceiveItemViewHolder) holder).titleTv.setTextColor(mContext.getResources().getColor(R.color.colorGreen));
                ((ReceiveItemViewHolder) holder).timeTv.setVisibility(View.VISIBLE);
                ((ReceiveItemViewHolder) holder).timeTv.setText(weatherInfo.getCreateTime());
                //将json数据转换成实体类
                Gson gson = new Gson();
                List<WeatherForecast> list = gson.fromJson(weatherInfo.getJsonInfo(), new TypeToken<List<WeatherForecast>>() {
                }.getType());
                for (int i = 0; i < list.size(); i++) {
                    if (i != 0) {
                        addLineView(((ReceiveItemViewHolder) holder).contentLayout);
                    }
                    WeatherForecast weatherForecast = list.get(i);
                    addTextToView(mContext.getString(R.string.date), DateUtils.timeStampToDate(String.valueOf(DateUtils.dayToTimestamp(weatherForecast.getDay())), "").substring(0,10), ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.daytime), WeatherCodeType.get(weatherForecast.getCondCodeD()).getName(), ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.night), WeatherCodeType.get(weatherForecast.getCondCodeN()).getName(), ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.temperature), weatherForecast.getTmpMin() + "°C~" + weatherForecast.getTmpMax() + "°C", ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.wind_direction), WindDirType.get(weatherForecast.getWindDir()).getName(), ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.wind_speed), weatherForecast.getWindSpd() + "km/h", ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.rain_probability), weatherForecast.getPop() + "%", ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.rainfall), weatherForecast.getPcpn() + "mm", ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.atmospheric_pressure), weatherForecast.getPres() + "hPa", ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.uv_level), UltravioletLevelType.get(weatherForecast.getUvIndex()).getName(), ((ReceiveItemViewHolder) holder).contentLayout);
                    addTextToView(mContext.getString(R.string.visibility), weatherForecast.getVis() + "km", ((ReceiveItemViewHolder) holder).contentLayout);
                }
            } else if (weatherInfo.getWeatherMode() == WeatherModeType.Warning.getValue()) {
                ((ReceiveItemViewHolder) holder).contentLayout.removeAllViews();
                ((ReceiveItemViewHolder) holder).contentLayout.setVisibility(View.VISIBLE);
                //灾害预警
                ((ReceiveItemViewHolder) holder).titleLayout.setVisibility(View.VISIBLE);
                ((ReceiveItemViewHolder) holder).tipsTv.setVisibility(View.GONE);
                ((ReceiveItemViewHolder) holder).titleImg.setImageResource(R.mipmap.dot_red_icon);
                ((ReceiveItemViewHolder) holder).titleTv.setText(WeatherModeType.Warning.getName());
                ((ReceiveItemViewHolder) holder).titleTv.setTextColor(mContext.getResources().getColor(R.color.colorRed));
                ((ReceiveItemViewHolder) holder).timeTv.setVisibility(View.VISIBLE);
                ((ReceiveItemViewHolder) holder).timeTv.setText(weatherInfo.getCreateTime());
                //将json数据转换成实体类
                Gson gson = new Gson();
                DisasterEarlyWarning disasterEarlyWarning = gson.fromJson(weatherInfo.getJsonInfo(), DisasterEarlyWarning.class);
                addTextToView(mContext.getString(R.string.disaster_title), disasterEarlyWarning.getTitle(), ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.alert_status), disasterEarlyWarning.getStatus(), ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.warning_level), DisasterLevelType.get(disasterEarlyWarning.getLevel()).getName(), ((ReceiveItemViewHolder) holder).contentLayout);
                addTextToView(mContext.getString(R.string.alert_Type), DisasterWarnType.get(disasterEarlyWarning.getType()).getName(), ((ReceiveItemViewHolder) holder).contentLayout);
            } else {
                //其他
                ((ReceiveItemViewHolder) holder).titleLayout.setVisibility(View.GONE);
                ((ReceiveItemViewHolder) holder).tipsTv.setVisibility(View.VISIBLE);
                ((ReceiveItemViewHolder) holder).contentLayout.setVisibility(View.GONE);
                ((ReceiveItemViewHolder) holder).timeTv.setVisibility(View.GONE);
            }
        } else if (holder instanceof SendItemViewHolder) {

            ((SendItemViewHolder) holder).contentTv.setText(weatherInfo.getJsonInfo());
            ((SendItemViewHolder) holder).timeTv.setVisibility(View.VISIBLE);
            ((SendItemViewHolder) holder).timeTv.setText(weatherInfo.getCreateTime());
        }
    }

    @Override
    public int getItemViewType(int position) {
        // 消息IO类型 IN：false 1；OUT：true 0
        return mList.get(position).getIoType();
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public interface OnItemClickListener {

    }

    public void upDateList(List<WeatherInfo> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void addItem(WeatherInfo weatherInfo) {
        mList.add(weatherInfo);
        notifyDataSetChanged();
    }

    /**
     * 添加天气项
     *
     * @param name
     * @param value
     * @param linearLayout
     */
    private void addTextToView(String name, String value, LinearLayout linearLayout) {
        String str = "<font color='#000000'>" + name + "</font>" + value;
        TextView content = new TextView(mContext);
        content.setText(Html.fromHtml(str));
        LinearLayout.LayoutParams contentLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        contentLayoutParams.setMargins(0, AppUtil.dip2px(mContext, 10), 0, 0);
        content.setLayoutParams(contentLayoutParams);
        linearLayout.addView(content);
    }

    private void addLineView(LinearLayout linearLayout) {
        View view = new View(mContext);
        LinearLayout.LayoutParams lineLayoutParams = new LinearLayout.LayoutParams(AppUtil.dip2px(mContext, 150), AppUtil.dip2px(mContext, 1));
        lineLayoutParams.setMargins(0, AppUtil.dip2px(mContext, 10), 0, 0);
        view.setLayoutParams(lineLayoutParams);
        view.setBackgroundColor(mContext.getResources().getColor(R.color.colorEditHideText));
        linearLayout.addView(view);
    }

    /**
     * 收到消息
     */
    class ReceiveItemViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout itemLayout;
        private ImageView headImg;
        private LinearLayout contentLayout;
        private LinearLayout titleLayout;
        private ImageView titleImg;
        private TextView titleTv;
        private TextView tipsTv;
        private TextView timeTv;


        public ReceiveItemViewHolder(View itemView) {
            super(itemView);
            itemLayout = itemView.findViewById(R.id.item_layout);
            headImg = itemView.findViewById(R.id.head_img);
            contentLayout = itemView.findViewById(R.id.content_layout);
            titleLayout = itemView.findViewById(R.id.title_layout);
            titleImg = itemView.findViewById(R.id.title_img);
            titleTv = itemView.findViewById(R.id.title_tv);
            tipsTv = itemView.findViewById(R.id.tips_tv);
            timeTv = itemView.findViewById(R.id.time_tv);
        }
    }

    /**
     * 发送消息
     */
    class SendItemViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout itemLayout;
        private TextView contentTv;
        private ImageView headImg;
        private TextView timeTv;

        public SendItemViewHolder(View itemView) {
            super(itemView);
            itemLayout = itemView.findViewById(R.id.item_layout);
            contentTv = itemView.findViewById(R.id.content_tv);
            headImg = itemView.findViewById(R.id.head_img);
            timeTv = itemView.findViewById(R.id.time_tv);
        }
    }
}
